import time

from django.shortcuts import get_object_or_404

from brispone.util.decorators import render_to, ajax_request
from brispone.models import Coupon
from brispone.util.sms import send

@render_to('brispone/coupon/list.html')
def list(request):
    coupons = Coupon.objects.all()
    return {'coupons':coupons}

@render_to('m/coupon/list.html')
def mlist(request):
    coupons = Coupon.objects.all()
    return {'coupons':coupons}

@render_to('brispone/coupon/detail.html')
def detail(request, coupon_id):
    coupon = get_object_or_404(Coupon, pk=coupon_id)
    return {'coupon':coupon}

@render_to('m/coupon/detail.html')
def mdetail(request, coupon_id):
    coupon = get_object_or_404(Coupon, pk=coupon_id)
    return {'coupon':coupon}

@ajax_request
def send_sms(request, coupon_id):
    #phone number
    result = ''
    coupon = get_object_or_404(Coupon, pk=coupon_id)
    if coupon.available():
        coupon.count_down()
        """
       msg = coupon.textmsg + ' ' + str(coupon.count)
       rst = send('61433898700', msg)
       if rst == 'q': 
            coupon.count_down()
            result = 'sent'
        else:
            result = 'fail'
        """
    time.sleep(2)
    coupon.count_down()    
    return {'result':'sent'}